package com.xw.vo;

import cn.hutool.json.JSONUtil;
import com.xw.enums.ResultEnum;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

public class FileResponseVO extends HashMap<String, Object> {
    private Map<String, Object> msgMap = new HashMap<>();

    public String error(int code, String msg) {
        FileResponseVO result = new FileResponseVO();
        msgMap.put("message", msg);
        result.put("uploaded", code);
        result.put("error", msgMap);
        return JSONUtil.toJsonStr(result);
    }

    public String error(ResultEnum resultEnum) {
        FileResponseVO result = new FileResponseVO();
        msgMap.put("message", resultEnum.getMsg());
        result.put("uploaded", resultEnum.getCode());
        result.put("error", msgMap);
        return JSONUtil.toJsonStr(result);
    }

    public String success(int code, String fileName, String url, String msg) {
        FileResponseVO result = new FileResponseVO();
        if (!ObjectUtils.isEmpty(msg)) {
            msgMap.put("message", msg);
            result.put("error", msgMap);
        }
        result.put("uploaded", code);
        result.put("fileName", fileName);
        result.put("url", url);
        return JSONUtil.toJsonStr(result);
    }

}
